﻿using System;
using System.Collections.Generic;
using System.DAL;
using System.Dto;
using System.Globalization;
using System.IBLL;
using System.IDAL;
using System.Linq;
using System.Models.Models;
using System.Text;
using System.Threading.Tasks;

namespace System.BLL
{
    public class ChargingManager : IChargingManager
    {
        public async Task AddOneExpenseInfo(ExpenseInfoToCostDeliveryDto dto)
        {
            using(IExpenseInformationService expenseInformationSvc = new ExpenseInformationService())
            {
                ExpenseInformation list = new ExpenseInformation()
                {
                    ExpenseName = dto.expenseName,
                    ExpenseType = dto.expenseType,
                    ExpenseNumber = double.Parse(dto.expenseNumber),
                    ExpenseState = false,
                    ExpenseBelong = dto.expenseBelong,
                    Remark = dto.remark
                };
                await expenseInformationSvc.CreateAsync(list);
            }
        }

        public List<ExpenseInfoToCostDeliveryDto> GetCostDeliveryDtoLists(int limit, int page, out int datalet, string expenseType, string expenseState, string paymentChannel)
        {
            using(IExpenseInformationService expenseInformationSvc = new ExpenseInformationService())
            {
                var data = expenseInformationSvc.GetAllAsync().Select(m => new ExpenseInfoToCostDeliveryDto()
                {
                    Id = m.Id,
                    createTime = m.CreateTime.ToString(),
                    remark = m.Remark,
                    expenseName = m.ExpenseName,
                    expenseType = m.ExpenseType,
                    expenseNumber = m.ExpenseNumber.ToString(),
                    expenseState = m.ExpenseState ? "已支付" : "待支付",
                    expenseBelong = m.ExpenseBelong,
                    paidInAmount = "--",
                    paymentChannel = "--",
                    payment = "--"
                }).ToList();
                using (IHouseholdService householdSvc = new HouseholdService())
                {
                    foreach (var list in data)
                    {

                        list.expenseBelong = householdSvc.GetOneByIdAsync(int.Parse(list.expenseBelong)).UserName;
                        if (list.expenseState == "已支付")
                        {
                            using (ICostDeliveryService costDeliverySvc = new CostDeliveryService())
                            {
                                var ls = costDeliverySvc.GetAllAsync().Where(m => m.ExpenseId == list.Id).FirstOrDefault();
                                list.paidInAmount = ls.PaidInAmount.ToString();
                                list.paymentChannel = ls.PaymentChannel;
                                list.payment = ls.Payment;
                            }
                        }
                    }
                }
                    

                if (!(expenseType == "" || expenseType == null))
                {
                    data = data.Where(m => m.expenseType.Contains(expenseType)).ToList();
                }
                if (!(expenseState == "" || expenseState == null))
                {
                    data = data.Where(m => m.expenseState.Contains(expenseState)).ToList();
                }
                if (!(paymentChannel == "" || paymentChannel == null))
                {
                    data = data.Where(m => m.paymentChannel.Contains(paymentChannel)).ToList();
                }

                datalet = data.Count();
                data = data.OrderBy(m=>m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;

            }
        }

        public List<string> GetExpenseTypeLists()
        {
            using(IExpenseInformationService expenseInformationSvc = new ExpenseInformationService())
            {
                var data = expenseInformationSvc.GetAllAsync().Select(m => m.ExpenseType).Distinct().ToList();
                return data;
            }
        }

        public ExpenseInfoToCostDeliveryDto GetOneExpenseInfo(int id)
        {
            using(IExpenseInformationService expenseInformationSvc = new ExpenseInformationService())
            {
                var list = expenseInformationSvc.GetOneByIdAsync(id);
                ExpenseInfoToCostDeliveryDto data = new ExpenseInfoToCostDeliveryDto()
                {
                    Id = list.Id,
                    expenseType = list.ExpenseType,
                    expenseBelong = list.ExpenseBelong,
                    expenseNumber = list.ExpenseNumber.ToString(),
                    expenseName = list.ExpenseName,
                    createTime = list.CreateTime.ToString()
                };
                return data;
            }
        }
    }
}
